package com.giveme5.common.view.dwz;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.json.JsonFormat;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.View;

import com.giveme5.common.exception.SimpleException;
import com.giveme5.common.view.ajax.AjaxResult;
import com.giveme5.common.view.ajax.AjaxUtil;

/**
 * 用于DwzAjax请求的视图
 * 
 * @author
 * 
 */
public class DwzView implements View {

	public DwzView(String value) {

	}

	public void render(HttpServletRequest req, HttpServletResponse resp, Object obj) throws Throwable {
		/**
		 * 处理异常时
		 */
		if (obj instanceof Throwable) {
			AjaxResult result = AjaxUtil.renderFail("");
			if (obj instanceof SimpleException) {
				result.setMessage(((SimpleException) obj).getMessage());
			} else {
				result.setMessage("程序异常,请怒抽程序猿!");
			}
			Mvcs.write(resp, result, null);
		}
		/**
		 * 处理正常时
		 */
		else {
			Mvcs.write(resp, obj, JsonFormat.compact());
		}
	}

}
